package com.sgcc.operate.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import com.sgcc.common.datascope.annotation.DataScope;
import com.sgcc.operate.domain.OGroupMng;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.sgcc.common.log.annotation.Log;
import com.sgcc.common.log.enums.BusinessType;
import com.sgcc.common.security.annotation.PreAuthorize;
import com.sgcc.operate.domain.OCrowdRule;
import com.sgcc.operate.service.IOCrowdRuleService;
import com.sgcc.common.core.web.controller.BaseController;
import com.sgcc.common.core.web.domain.AjaxResult;
import com.sgcc.common.core.utils.poi.ExcelUtil;
import com.sgcc.common.core.web.page.TableDataInfo;

/**
 * 人群规则定义Controller
 *
 * @author sgcc
 * @date 2021-03-23
 */
@RestController
@RequestMapping("/crowdrule")
public class OCrowdRuleController extends BaseController {
    @Autowired
    private IOCrowdRuleService oCrowdRuleService;

    /**
     * 查询人群规则定义列表
     */
    //@PreAuthorize(hasPermi = "operate:rule:list")
    @GetMapping("/list")
    @DataScope(deptAlias = "b")
    public TableDataInfo list(OCrowdRule oCrowdRule) {
        startPage();
        List<OCrowdRule> list = oCrowdRuleService.selectOCrowdRuleList(oCrowdRule);
        return getDataTable(list);
    }

    /**
     * 导出人群规则定义列表
     */
    //@PreAuthorize(hasPermi = "operate:rule:export")
    @Log(title = "人群规则定义", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OCrowdRule oCrowdRule) throws IOException {
        List<OCrowdRule> list = oCrowdRuleService.selectOCrowdRuleList(oCrowdRule);
        ExcelUtil<OCrowdRule> util = new ExcelUtil<OCrowdRule>(OCrowdRule.class);
        util.exportExcel(response, list, "rule");
    }

    /**
     * 获取人群规则定义详细信息
     */
    //@PreAuthorize(hasPermi = "operate:rule:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id) {
        return AjaxResult.success(oCrowdRuleService.selectOCrowdRuleById(id));
    }

    /**
     * 新增人群规则定义
     */
    //@PreAuthorize(hasPermi = "operate:rule:add")
    @Log(title = "人群规则定义", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OCrowdRule oCrowdRule) {
        if (oCrowdRuleService.judgeName(oCrowdRule.getId(), oCrowdRule.getRulename())) {
            return AjaxResult.error("属性名称已经存在！请重新输入。");
        }

        return toAjax(oCrowdRuleService.insertOCrowdRule(oCrowdRule));
    }

    /**
     * 修改人群规则定义
     */
    //@PreAuthorize(hasPermi = "operate:rule:edit")
    @Log(title = "人群规则定义", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OCrowdRule oCrowdRule) {
        return toAjax(oCrowdRuleService.updateOCrowdRule(oCrowdRule));
    }

    /**
     * 删除人群规则定义
     */
    //@PreAuthorize(hasPermi = "operate:rule:remove")
    @Log(title = "人群规则定义", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids) {
        return toAjax(oCrowdRuleService.deleteOCrowdRuleByIds(ids));
    }


    /**
     * 修改状态
     *
     * @param ids
     * @param crowdStatus
     * @return
     */
    @PostMapping(value = "updateStatus")
    public AjaxResult updateStatus(String ids, String crowdStatus) {
        if (StringUtils.isBlank(ids)) {
            return AjaxResult.error("请选择一条信息进行操作");
        }
        String[] crowdRuleIdList = ids.split(",");
        for (String id : crowdRuleIdList) {
            OCrowdRule oCrowdRule1 = oCrowdRuleService.selectOCrowdRuleById(id);
            if (oCrowdRule1 == null) {
                continue;
            }
            if (crowdStatus.equals("02")) {
                if (oCrowdRule1.getCrowdstatus().equals("02")) {
                    return AjaxResult.error("该人群规则已启用");
                } else if (oCrowdRule1.getCrowdstatus().equals("03")) {
                    return AjaxResult.error("该人群规则已作废不能启用");
                }

            }

            if (crowdStatus.equals("03") && oCrowdRule1.getCrowdstatus().equals("03")) {
                return AjaxResult.error("该人群规则已作废");
            }

        }


        for (String id : crowdRuleIdList) {
            OCrowdRule oCrowdRule = new OCrowdRule();
            oCrowdRule.setId(id);
            oCrowdRule.setCrowdstatus(crowdStatus);
            oCrowdRuleService.updateOCrowdRule(oCrowdRule);
        }
        return AjaxResult.success();
    }

    /**
     * 获取人群规则属性字段
     *
     * @param ruleAttrId
     * @return
     */
    @GetMapping(value = "getFieldByRuleAttrId")
    public AjaxResult getFieldByRuleAttrId(String ruleAttrId) {
        return AjaxResult.success(oCrowdRuleService.getFieldByRuleAttrId(ruleAttrId));
    }

    /**
     * 查询群组管理列表
     */
    //@PreAuthorize(hasPermi = "operate:groupmng:list")
    @GetMapping("/optionselect")
    public AjaxResult optionselect(OCrowdRule oCrowdRule) {
        return AjaxResult.success(oCrowdRuleService.selectOCrowdRuleList(oCrowdRule));
    }

}
